import { join } from 'path'
import {
  __PATH,
  ForwardMsg,
  Read_player,
  shijianc,
  Add_money,
  existplayer,
  Add_najie_thing,
  exist_najie_thing,
  zd_battle,
  data,
  Show,
  Read_tiandibang,
  Write_tiandibang,
  getLastbisai
} from '../../model/index.js'
import { readdirSync } from 'fs'
import { plugin, puppeteer } from '../../../import.js'
export class Tiandibang extends plugin {
  constructor() {
    super({
      name: 'Tiandibang',
      dsc: '交易模块',
      event: 'message',
      priority: 600,
      rule: [
        {
          reg: /^(#|\/)天地榜$/,
          fnc: 'my_point'
        },
        {
          reg: /^(#|\/)比试$/,
          fnc: 'pk'
        },
        {
          reg: /^(#|\/)更新属性$/,
          fnc: 'update_jineng'
        },
        {
          reg: /^(#|\/)清空积分/,
          fnc: 'bd_jiesuan'
        },
        {
          reg: /^(#|\/)报名比赛/,
          fnc: 'cansai'
        },
        {
          reg: /^(#|\/)天地堂/,
          fnc: 'tianditang'
        },
        {
          reg: /^(#|\/)积分兑换(.*)$/,
          fnc: 'duihuan'
        }
      ]
    })
    this.task = {
      cron: data.test.saiji,
      name: 're_bangdang',
      fnc: () => this.re_bangdang()
    }
  }
  async re_bangdang() {
    let File = readdirSync(__PATH.player_path)
    File = File.filter((file) => file.endsWith('.json'))
    let File_length = File.length
    let temp = []
    let t
    for (let k = 0; k < File_length; k++) {
      let this_qq = File[k].replace('.json', '')
      this_qq = this_qq
      let player = await Read_player(this_qq)

      const Level_list = data.Level_list()

      let level_id = Level_list.find(
        (item) => item.level_id == player.level_id
      ).level_id
      temp[k] = {
        name: player.name,
        境界: level_id,
        攻击: player.攻击,
        防御: player.防御,
        now_bool: player.血量上限,
        暴击率: player.暴击率,
        talent: player.talent,
        法球倍率: player.talent.法球倍率,
        studytheskill: player.studytheskill,
        魔道值: player.魔道值,
        神石: player.神石,
        qq: this_qq,
        次数: 3,
        积分: 0
      }
    }
    for (let i = 0; i < File_length - 1; i++) {
      let count = 0
      for (let j = 0; j < File_length - i - 1; j++) {
        if (temp[j].积分 < temp[j + 1].积分) {
          t = temp[j]
          temp[j] = temp[j + 1]
          temp[j + 1] = t
          count = 1
        }
      }
      if (count == 0) break
    }
    await Write_tiandibang(temp)
    return false
  }

  async duihuan(e) {
    let date = new Date()
    let n = date.getDay()
    if (n != 0) {
      e.reply(`物品筹备中，等到周日再来兑换吧`)
      return false
    }

    let user_id = e.user_id
    //查看存档
    let ifexistplay = await existplayer(user_id)
    if (!ifexistplay) return false
    let reg = new RegExp(/积分兑换/)
    let msg = e.msg.replace(reg, '')
    msg = msg.replace(/^(#|\/)/, '')
    let thing_name = msg.replace('积分兑换', '')
    let ifexist = data.tianditang().find((item) => item.name == thing_name)
    if (!ifexist) {
      e.reply(`天地堂还没有这样the东西:${thing_name}`)
      return false
    }
    let tiandibang
    tiandibang = await Read_tiandibang()
    let m = tiandibang.length
    let i
    for (m = 0; m < tiandibang.length; m++) {
      if (tiandibang[m].qq == user_id) {
        break
      }
    }
    if (m == tiandibang.length) {
      e.reply('请先报名!')
      return false
    }
    for (i = 0; i < data.tianditang.length; i++) {
      if (thing_name == data.tianditang[i].name) {
        break
      }
    }
    if (tiandibang[m].积分 < data.tianditang[i].积分) {
      e.reply(
        `积分不足,还需${
          data.tianditang[i].积分 - tiandibang[m].积分
        }积分兑换${thing_name}`
      )
      return false
    }
    tiandibang[m].积分 -= data.tianditang[i].积分
    await Add_najie_thing(user_id, thing_name, data.tianditang[i].class, 1)
    await Write_tiandibang(tiandibang)
    e.reply([
      `兑换成功!  获得[${thing_name}],剩余[${tiandibang[m].积分}]积分  `,
      '\n可以在【我the纳戒】中查看'
    ])
    return false
  }

  async tianditang(e) {
    let user_id = e.user_id
    //查看存档
    let ifexistplay = await existplayer(user_id)
    if (!ifexistplay) return false
    let tiandibang
    try {
      tiandibang = await Read_tiandibang()
    } catch {
      //没有表要先建立一个！
      await Write_tiandibang([])
      tiandibang = await Read_tiandibang()
    }
    let m = tiandibang.length
    for (m = 0; m < tiandibang.length; m++) {
      if (tiandibang[m].qq == user_id) {
        break
      }
    }
    if (m == tiandibang.length) {
      e.reply('请先报名!')
      return false
    }
    let img = await get_tianditang_img(e, tiandibang[m].积分)
    e.reply(img)
    return false
  }

  async cansai(e) {
    let user_id = e.user_id
    //查看存档
    let ifexistplay = await existplayer(user_id)
    if (!ifexistplay) return false
    let tiandibang
    try {
      tiandibang = await Read_tiandibang()
    } catch {
      //没有表要先建立一个！
      await Write_tiandibang([])
      tiandibang = await Read_tiandibang()
    }
    let x = tiandibang.length
    for (let i = 0; i < tiandibang.length; i++) {
      if (tiandibang[i].qq == user_id) {
        x = i
        break
      }
    }
    if (x == tiandibang.length) {
      let player = await Read_player(user_id)
      let level_id = data
        .Level_list()
        .find((item) => item.level_id == player.level_id).level_id
      let A_player = {
        name: player.name,
        境界: level_id,
        攻击: player.攻击,
        防御: player.防御,
        now_bool: player.血量上限,
        暴击率: player.暴击率,
        talent: player.talent,
        法球倍率: player.talent.法球倍率,
        studytheskill: player.studytheskill,
        qq: user_id,
        次数: 0,
        积分: 0
      }

      tiandibang.push(A_player)
      await Write_tiandibang(tiandibang)
      e.reply('参赛成功!')
      return false
    } else {
      e.reply('你已经参赛了!')
      return false
    }
  }

  async my_point(e) {
    let user_id = e.user_id
    //查看存档
    let ifexistplay = await existplayer(user_id)
    if (!ifexistplay) return false
    let tiandibang
    try {
      tiandibang = await Read_tiandibang()
    } catch {
      //没有表要先建立一个！
      await Write_tiandibang([])
      tiandibang = await Read_tiandibang()
    }
    let x = tiandibang.length
    let l = 10
    let msg = ['***天地榜(每日免费三次)***\n       周一0点清空积分']
    for (let i = 0; i < tiandibang.length; i++) {
      if (tiandibang[i].qq == user_id) {
        x = i
        break
      }
    }
    if (x == tiandibang.length) {
      e.reply('请先报名!')
      return false
    }
    if (l > tiandibang.length) {
      l = tiandibang.length
    }
    if (x < l) {
      for (let m = 0; m < l; m++) {
        msg.push(
          '名次：' +
            (m + 1) +
            '\nname：' +
            tiandibang[m].name +
            '\n积分：' +
            tiandibang[m].积分
        )
      }
    } else if (x >= l && tiandibang.length - x < l) {
      for (let m = tiandibang.length - l; m < tiandibang.length; m++) {
        msg.push(
          '名次：' +
            (m + 1) +
            '\nname：' +
            tiandibang[m].name +
            '\n积分：' +
            tiandibang[m].积分
        )
      }
    } else {
      for (let m = x - 5; m < x + 5; m++) {
        msg.push(
          '名次：' +
            (m + 1) +
            '\nname：' +
            tiandibang[m].name +
            '\n积分：' +
            tiandibang[m].积分
        )
      }
    }
    await ForwardMsg(e, msg)
    return false
  }

  async pk(e) {
    let user_id = e.user_id
    let ifexistplay = await existplayer(user_id)
    if (!ifexistplay) return false
    //获取游戏状态
    let game_action = await redis.get(
      'xiuxian@1.4.0:' + user_id + ':game_action'
    )
    //防止继续其他娱乐行为
    if (game_action == '0') {
      e.reply('修仙：游戏进行中...')
      return false
    }
    //查询redis中the人物动作
    let action = JSON.parse(
      await redis.get('xiuxian@1.4.0:' + user_id + ':action')
    )
    if (action != null) {
      //人物有动作查询动作结束时间
      let action_end_time = action.end_time
      let now_time = new Date().getTime()
      if (now_time <= action_end_time) {
        let m = (action_end_time - now_time) / 1000 / 60
        let s = (action_end_time - now_time - m * 60 * 1000) / 1000
        e.reply('正在' + action.action + '中,剩余时间:' + m + '分' + s + '秒')
        return false
      }
    }
    let tiandibang
    try {
      tiandibang = await Read_tiandibang()
    } catch {
      //没有表要先建立一个！
      await Write_tiandibang([])
      tiandibang = await Read_tiandibang()
    }
    let x = tiandibang.length
    for (let m = 0; m < tiandibang.length; m++) {
      if (tiandibang[m].qq == user_id) {
        x = m
        break
      }
    }
    if (x == tiandibang.length) {
      e.reply('请先报名!')
      return false
    }
    let last_msg = []
    let atk = 1
    let def = 1
    let blood = 1
    let now = new Date()
    let nowTime = now.getTime() //获取当前日期the时间戳
    let Today = await shijianc(nowTime)
    let lastbisai_time = await getLastbisai(user_id) //获得上次签到日期
    if (!lastbisai_time) return
    if (
      Today.Y != lastbisai_time.Y ||
      Today.M != lastbisai_time.M ||
      Today.D != lastbisai_time.D
    ) {
      await redis.set('xiuxian@1.4.0:' + user_id + ':lastbisai_time', nowTime) //redis设置签到时间
      tiandibang[x].次数 = 3
    }
    if (
      Today.Y == lastbisai_time.Y &&
      Today.M == lastbisai_time.M &&
      Today.D == lastbisai_time.D &&
      tiandibang[x].次数 < 1
    ) {
      let zbl = await exist_najie_thing(user_id, '摘榜令', '道具')
      if (zbl) {
        tiandibang[x].次数 = 1
        await Add_najie_thing(user_id, '摘榜令', '道具', -1)
        last_msg.push(`${tiandibang[x].name}使用了摘榜令\n`)
      } else {
        e.reply('今日挑战次数用光了,请明日再来吧')
        return false
      }
    }
    Write_tiandibang(tiandibang)
    let lingshi
    tiandibang = await Read_tiandibang()
    if (x != 0) {
      let k
      for (k = x - 1; k >= 0; k--) {
        if (tiandibang[x].境界 > 41) break
        else {
          if (tiandibang[k].境界 > 41) {
            continue
          } else break
        }
      }
      let B_player
      if (k != -1) {
        if (tiandibang[k].攻击 / tiandibang[x].攻击 > 2) {
          atk = 2
          def = 2
          blood = 2
        } else if (tiandibang[k].攻击 / tiandibang[x].攻击 > 1.6) {
          atk = 1.6
          def = 1.6
          blood = 1.6
        } else if (tiandibang[k].攻击 / tiandibang[x].攻击 > 1.3) {
          atk = 1.3
          def = 1.3
          blood = 1.3
        }
        B_player = {
          name: tiandibang[k].name,
          攻击: tiandibang[k].攻击,
          防御: tiandibang[k].防御,
          now_bool: tiandibang[k].now_bool,
          暴击率: tiandibang[k].暴击率,
          studytheskill: tiandibang[k].studytheskill,
          talent: tiandibang[k].talent,
          法球倍率: tiandibang[k].法球倍率
        }
      }
      let A_player = {
        name: tiandibang[x].name,
        攻击: tiandibang[x].攻击 * atk,
        防御: tiandibang[x].防御 * def,
        now_bool: tiandibang[x].now_bool * blood,
        暴击率: tiandibang[x].暴击率,
        studytheskill: tiandibang[x].studytheskill,
        talent: tiandibang[x].talent,
        法球倍率: tiandibang[x].法球倍率
      }
      if (k == -1) {
        atk = 0.8 + 0.4 * Math.random()
        def = 0.8 + 0.4 * Math.random()
        blood = 0.8 + 0.4 * Math.random()
        B_player = {
          name: '灵修兽',
          攻击: tiandibang[x].攻击 * atk,
          防御: tiandibang[x].防御 * def,
          now_bool: tiandibang[x].now_bool * blood,
          暴击率: tiandibang[x].暴击率,
          studytheskill: tiandibang[x].studytheskill,
          talent: tiandibang[x].talent,
          法球倍率: tiandibang[x].法球倍率
        }
      }
      let Data_battle = await zd_battle(A_player, B_player)
      let msg = Data_battle.msg
      let A_win = `${A_player.name}击败了${B_player.name}`
      let B_win = `${B_player.name}击败了${A_player.name}`
      if (msg.find((item) => item == A_win)) {
        if (k == -1) {
          tiandibang[x].积分 += 1500
          lingshi = tiandibang[x].积分 * 8
        } else {
          tiandibang[x].积分 += 2000
          lingshi = tiandibang[x].积分 * 4
        }
        tiandibang[x].次数 -= 1
        last_msg.push(
          `${A_player.name}击败了[${B_player.name}],当前积分[${tiandibang[x].积分}],获得了[${lingshi}]money`
        )
        Write_tiandibang(tiandibang)
      } else if (msg.find((item) => item == B_win)) {
        if (k == -1) {
          tiandibang[x].积分 += 800
          lingshi = tiandibang[x].积分 * 6
        } else {
          tiandibang[x].积分 += 1000
          lingshi = tiandibang[x].积分 * 2
        }
        tiandibang[x].次数 -= 1
        last_msg.push(
          `${A_player.name}被[${B_player.name}]打败了,当前积分[${tiandibang[x].积分}],获得了[${lingshi}]money`
        )
        Write_tiandibang(tiandibang)
      } else {
        e.reply(`战斗过程出错`)
        return false
      }
      await Add_money(user_id, lingshi)
      if (msg.length > 50) {
        console.log('通过')
      } else {
        await ForwardMsg(e, msg)
      }
      e.reply(last_msg)
    } else {
      let A_player = {
        name: tiandibang[x].name,
        攻击: tiandibang[x].攻击,
        防御: tiandibang[x].防御,
        now_bool: tiandibang[x].now_bool,
        暴击率: tiandibang[x].暴击率,
        studytheskill: tiandibang[x].studytheskill,
        talent: tiandibang[x].talent,
        法球倍率: tiandibang[x].法球倍率
      }
      atk = 0.8 + 0.4 * Math.random()
      def = 0.8 + 0.4 * Math.random()
      blood = 0.8 + 0.4 * Math.random()
      let B_player = {
        name: '灵修兽',
        攻击: tiandibang[x].攻击 * atk,
        防御: tiandibang[x].防御 * def,
        now_bool: tiandibang[x].now_bool * blood,
        暴击率: tiandibang[x].暴击率,
        studytheskill: tiandibang[x].studytheskill,
        talent: tiandibang[x].talent,
        法球倍率: tiandibang[x].法球倍率
      }
      let Data_battle = await zd_battle(A_player, B_player)
      let msg = Data_battle.msg
      let A_win = `${A_player.name}击败了${B_player.name}`
      let B_win = `${B_player.name}击败了${A_player.name}`
      if (msg.find((item) => item == A_win)) {
        tiandibang[x].积分 += 1500
        tiandibang[x].次数 -= 1
        lingshi = tiandibang[x].积分 * 8
        last_msg.push(
          `${A_player.name}击败了[${B_player.name}],当前积分[${tiandibang[x].积分}],获得了[${lingshi}]money`
        )
        Write_tiandibang(tiandibang)
      } else if (msg.find((item) => item == B_win)) {
        tiandibang[x].积分 += 800
        tiandibang[x].次数 -= 1
        lingshi = tiandibang[x].积分 * 6
        last_msg.push(
          `${A_player.name}被[${B_player.name}]打败了,当前积分[${tiandibang[x].积分}],获得了[${lingshi}]money`
        )
        Write_tiandibang(tiandibang)
      } else {
        e.reply(`战斗过程出错`)
        return false
      }
      await Add_money(user_id, lingshi)
      if (msg.length > 50) {
        console.log('通过')
      } else {
        await ForwardMsg(e, msg)
      }
      e.reply(last_msg)
    }
    tiandibang = await Read_tiandibang()
    let t
    for (let i = 0; i < tiandibang.length - 1; i++) {
      let count = 0
      for (let j = 0; j < tiandibang.length - i - 1; j++) {
        if (tiandibang[j].积分 < tiandibang[j + 1].积分) {
          t = tiandibang[j]
          tiandibang[j] = tiandibang[j + 1]
          tiandibang[j + 1] = t
          count = 1
        }
      }
      if (count == 0) break
    }
    Write_tiandibang(tiandibang)
    return false
  }

  async update_jineng(e) {
    let user_id = e.user_id
    if (!e.isGroup) {
      e.reply('此功能暂时不开放私聊')
      return false
    }

    //查看存档
    let ifexistplay = await existplayer(user_id)
    if (!ifexistplay) return false
    let tiandibang
    try {
      tiandibang = await Read_tiandibang()
    } catch {
      //没有表要先建立一个！
      await Write_tiandibang([])
      tiandibang = await Read_tiandibang()
    }
    let m = tiandibang.length
    for (m = 0; m < tiandibang.length; m++) {
      if (tiandibang[m].qq == user_id) {
        break
      }
    }
    if (m == tiandibang.length) {
      e.reply('请先报名!')
      return false
    }
    let player = await Read_player(user_id)
    let level_id = data
      .Level_list()
      .find((item) => item.level_id == player.level_id).level_id
    tiandibang[m].name = player.name
    tiandibang[m].境界 = level_id
    tiandibang[m].攻击 = player.攻击
    tiandibang[m].防御 = player.防御
    tiandibang[m].now_bool = player.血量上限
    tiandibang[m].暴击率 = player.暴击率
    tiandibang[m].studytheskill = player.studytheskill
    ;(tiandibang[m].talent = player.talent),
      (tiandibang[m].法球倍率 = player.talent.法球倍率),
      Write_tiandibang(tiandibang)
    tiandibang = await Read_tiandibang()
    tiandibang[m].暴击率 = Math.trunc(tiandibang[m].暴击率 * 100)
    let msg = []
    msg.push(
      '名次：' +
        (m + 1) +
        '\nname：' +
        tiandibang[m].name +
        '\n攻击：' +
        tiandibang[m].攻击 +
        '\n防御：' +
        tiandibang[m].防御 +
        '\n血量：' +
        tiandibang[m].now_bool +
        '\n暴击：' +
        tiandibang[m].暴击率 +
        '%\n积分：' +
        tiandibang[m].积分
    )
    await ForwardMsg(e, msg)
    return false
  }

  async bd_jiesuan(e) {
    if (!e.isMaster) {
      e.reply('只有主人可以执行操作')
      return false
    }
    try {
      await Read_tiandibang()
    } catch {
      //没有表要先建立一个！
      await Write_tiandibang([])
    }
    await re_bangdang()
    e.reply('积分已经重置！')
    return false
  }
}

async function get_tianditang_img(e, jifen) {
  let player = await Read_player(e.user_id)
  const data1 = await new Show().get_tianditangData({
    name: player.name,
    jifen,
    commodities_list: data.tianditang
  })
  return puppeteer.screenshot('tianditang', data1)
}

/**
 *
 * @returns
 */
async function re_bangdang() {
  let File = readdirSync(__PATH.player_path).filter((file) =>
    file.endsWith('.json')
  )
  let File_length = File.length
  let temp = []
  let t
  for (let k = 0; k < File_length; k++) {
    let this_qq = File[k].replace('.json', '')
    this_qq = this_qq
    let player = await Read_player(this_qq)
    let level_id = data
      .Level_list()
      .find((item) => item.level_id == player.level_id).level_id
    temp[k] = {
      name: player.name,
      境界: level_id,
      攻击: player.攻击,
      防御: player.防御,
      now_bool: player.血量上限,
      暴击率: player.暴击率,
      talent: player.talent,
      法球倍率: player.talent.法球倍率,
      studytheskill: player.studytheskill,
      魔道值: player.魔道值,
      神石: player.神石,
      qq: this_qq,
      次数: 3,
      积分: 0
    }
  }
  for (let i = 0; i < File_length - 1; i++) {
    let count = 0
    for (let j = 0; j < File_length - i - 1; j++) {
      if (temp[j].积分 < temp[j + 1].积分) {
        t = temp[j]
        temp[j] = temp[j + 1]
        temp[j + 1] = t
        count = 1
      }
    }
    if (count == 0) break
  }
  await Write_tiandibang(temp)
  return false
}
